home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / less_237.zip / less_237 / main.c < prev    next >
C/C++ Source or Header  |  1994-09-21  |  7KB  |  330 lines

  1. /*
  2.  * Copyright (c) 1984,1985,1989,1994  Mark Nudelman
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice in the documentation and/or other materials provided with 
  12.  *    the distribution.
  13.  *
  14.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
  15.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  17.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
  18.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  19.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
  20.  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
  21.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  22.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
  23.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
  24.  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25.  */
  26.  
  27.  
  28. /*
  29.  * Entry point, initialization, miscellaneous routines.
  30.  */
  31.  
  32. #include "less.h"
  33. #include "position.h"
  34.  
  35. public int    ispipe;
  36. public char *    every_first_cmd = NULL;
  37. public int    new_file;
  38. public int    is_tty;
  39. public IFILE    curr_ifile = NULL_IFILE;
  40. public IFILE    old_ifile = NULL_IFILE;
  41. public struct scrpos initial_scrpos;
  42. public int    any_display = 0;
  43. public int    wscroll;
  44. public char *    progname;
  45. public int    quitting;
  46.  
  47. extern int    file;
  48. extern int    fd0;
  49. extern int    quit_at_eof;
  50. extern int    cbufs;
  51. extern int    errmsgs;
  52. extern int    screen_trashed;
  53. extern int    force_open;
  54.  
  55. #if LOGFILE
  56. public int    logfile = -1;
  57. public int    force_logfile = 0;
  58. public char *    namelogfile = NULL;
  59. #endif
  60.  
  61. #if EDITOR
  62. public char *    editor;
  63. public char *    editproto;
  64. #endif
  65.  
  66. #if TAGS
  67. extern char *    tagfile;
  68. extern char *    tagoption;
  69. extern int    jump_sline;
  70. #endif
  71.  
  72.  
  73.  
  74. /*
  75.  * Entry point.
  76.  */
  77. int
  78. main(argc, argv)
  79.     int argc;
  80.     char *argv[];
  81. {
  82.     IFILE ifile;
  83.  
  84.     progname = *argv++;
  85.  
  86.     /*
  87.      * Process command line arguments and LESS environment arguments.
  88.      * Command line arguments override environment arguments.
  89.      */
  90.     init_prompt();
  91.     init_charset();
  92.     init_option();
  93.     scan_option(getenv("LESS"));
  94.  
  95. #define    isoptstring(s)    (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
  96.     while (--argc > 0 && (isoptstring(argv[0]) || isoptpending()))
  97.         scan_option(*argv++);
  98. #undef isoptstring
  99.  
  100.     if (isoptpending())
  101.     {
  102.         /*
  103.          * Last command line option was a flag requiring a
  104.          * following string, but there was no following string.
  105.          */
  106.         nopendopt();
  107.         quit(0);
  108.     }
  109.  
  110. #if USERFILE
  111.     /*
  112.      * Try to use the lesskey file "$HOME/.less".
  113.      */
  114.     add_hometable();
  115. #endif
  116. #if EDITOR
  117.     editor = getenv("EDITOR");
  118.     if (editor == NULL || *editor == '\0')
  119.         editor = EDIT_PGM;
  120.     editproto = getenv("LESSEDIT");
  121.     if (editproto == NULL || *editproto == '\0')
  122.         editproto = "%E ?lm+%lm. %f";
  123. #endif
  124.  
  125.     /*
  126.      * Call get_ifile with all the command line filenames
  127.      * to "register" them with the ifile system.
  128.      */
  129.     ifile = NULL_IFILE;
  130.     while (--argc >= 0)
  131.     {
  132. #if MSOFTC
  133.         /*
  134.          * Because the "shell" doesn't expand filename patterns,
  135.          * treat each argument as a filename pattern rather than
  136.          * a single filename.  
  137.          * Expand the pattern and iterate over the expanded list.
  138.          */
  139.         {
  140.             struct textlist tlist;
  141.             char *gfilename;
  142.             char *filename;
  143.             
  144.             gfilename = glob(*argv++);
  145.             init_textlist(&tlist, gfilename);
  146.             filename = NULL;
  147.             while ((filename = forw_textlist(&tlist, filename)) != NULL)
  148.                 ifile = get_ifile(filename, ifile);
  149.             free(gfilename);
  150.         }
  151. #else
  152.         ifile = get_ifile(*argv++, ifile);
  153. #endif
  154.     }
  155.     /*
  156.      * Set up terminal, etc.
  157.      */
  158.     is_tty = isatty(1);
  159.     if (!is_tty)
  160.     {
  161.         /*
  162.          * Output is not a tty.
  163.          * Just copy the input file(s) to output.
  164.          */
  165.         if (nifile() == 0)
  166.         {
  167.             if (edit_stdin() == 0)
  168.                 cat_file();
  169.         } else if (edit_first() == 0)
  170.         {
  171.             do {
  172.                 cat_file();
  173.             } while (edit_next(1) == 0);
  174.         }
  175.         quit(0);
  176.     }
  177.  
  178.     init_mark();
  179.     raw_mode(1);
  180.     get_term();
  181.     get_editkeys();
  182.     open_getchr();
  183.     cmd_init();
  184.  
  185.     init_signals(1);
  186.  
  187.     /*
  188.      * Select the first file to examine.
  189.      */
  190. #if TAGS
  191.     if (tagoption != NULL)
  192.     {
  193.         /*
  194.          * A -t option was given.
  195.          * Verify that no filenames were also given.
  196.          * Edit the file selected by the "tags" search,
  197.          * and search for the proper line in the file.
  198.          */
  199.         if (nifile() > 0)
  200.         {
  201.             error("No filenames allowed with -t option", NULL_PARG);
  202.             quit(1);
  203.         }
  204.         findtag(tagoption);
  205.         if (tagfile == NULL)
  206.             quit(1);
  207.         if (edit(tagfile))  /* Edit file which contains the tag */
  208.             quit(1);
  209.         /*
  210.          * Search for the line which contains the tag.
  211.          * Set up initial_scrpos so we display that line.
  212.          */
  213.         initial_scrpos.pos = tagsearch();
  214.         if (initial_scrpos.pos == NULL_POSITION)
  215.             quit(1);
  216.         initial_scrpos.ln = jump_sline;
  217.     } else
  218. #endif
  219.     if (nifile() == 0)
  220.     {
  221.         if (edit_stdin())  /* Edit standard input */
  222.             quit(1);
  223.     } else 
  224.     {
  225.         if (edit_first())  /* Edit first valid file in cmd line */
  226.             quit(1);
  227.     }
  228.  
  229.     init();
  230.     commands();
  231.     quit(0);
  232.     /*NOTREACHED*/
  233. }
  234.  
  235. /*
  236.  * Copy a string, truncating to the specified length if necessary.
  237.  * Unlike strncpy(), the resulting string is guaranteed to be null-terminated.
  238.  */
  239.     public void
  240. strtcpy(to, from, len)
  241.     char *to;
  242.     char *from;
  243.     unsigned int len;
  244. {
  245.     strncpy(to, from, len);
  246.     to[len-1] = '\0';
  247. }
  248.  
  249. /*
  250.  * Copy a string to a "safe" place
  251.  * (that is, to a buffer allocated by calloc).
  252.  */
  253.     public char *
  254. save(s)
  255.     char *s;
  256. {
  257.     register char *p;
  258.  
  259.     p = (char *) ecalloc(strlen(s)+1, sizeof(char));
  260.     strcpy(p, s);
  261.     return (p);
  262. }
  263.  
  264. /*
  265.  * Allocate memory.
  266.  * Like calloc(), but never returns an error (NULL).
  267.  */
  268.     public VOID_POINTER
  269. ecalloc(count, size)
  270.     int count;
  271.     unsigned int size;
  272. {
  273.     register VOID_POINTER p;
  274.  
  275.     p = (VOID_POINTER) calloc(count, size);
  276.     if (p != NULL)
  277.         return (p);
  278.     error("Cannot allocate memory", NULL_PARG);
  279.     quit(1);
  280.     /*NOTREACHED*/
  281. }
  282.  
  283. /*
  284.  * Skip leading spaces in a string.
  285.  */
  286.     public char *
  287. skipsp(s)
  288.     register char *s;
  289. {
  290.     while (*s == ' ' || *s == '\t')    
  291.         s++;
  292.     return (s);
  293. }
  294.  
  295. /*
  296.  * Exit the program.
  297.  */
  298.     public void
  299. quit(status)
  300.     int status;
  301. {
  302.     static int save_status;
  303.  
  304.     /*
  305.      * Put cursor at bottom left corner, clear the line,
  306.      * reset the terminal modes, and exit.
  307.      */
  308.     if (status < 0)
  309.         status = save_status;
  310.     else
  311.         save_status = status;
  312.     quitting = 1;
  313.     edit((char*)NULL);
  314.     if (any_display)
  315.         clear_bot();
  316.     deinit();
  317.     flush();
  318.     raw_mode(0);
  319. #if MSOFTC
  320.     /* 
  321.      * If we don't close 2, we get some garbage from
  322.      * 2's buffer when it flushes automatically.
  323.      * I cannot track this one down  RB
  324.      * The same bug shows up if we use ^C^C to abort.
  325.      */
  326.     close(2);
  327. #endif
  328.     exit(status);
  329. }
  330.